#include "include.h"
#include "bsp_eq.h"

u8 eq_rx_buf[EQ_BUFFER_LEN];
#if EQ_DBG_IN_UART
eq_dbg_cb_t eq_dbg_cb;
static u8 eq_tx_buf[EQ_TX_BUF_LEN];
static const char tbl_eq_version[10] = {'E', 'Q', '1', '*', 6, 0, 12, 4, 5, 0};
static const char tbl_drc_version[10] = {'D', 'R', '2', '*', 6, 0, 4, 0, 0, 0};

AT(.text.dac_eq_drc.dbg_tx)
static u8 check_sum(u8 *buf, u16 size)
{
    u32 i, sum = 0;
    for (i = 0; i < size; i++) {
        sum += buf[i];
    }
    return (u8)(-sum);
}

AT(.text.dac_eq_drc.dbg_tx)
static void tx_ack(uint8_t *packet, uint16_t len)
{
    delay_5ms(1);   //延时一段时间再ack
    if (eq_dbg_cb.rx_type) {
        huart_putcs(packet, len);
    }
    eq_dbg_cb.rx_type = 0;
}

#if EFFECT_SET_IN_UART
void dsp_effect_tx_ack(u8 *head_tag, u8 ack)
{
    head_tag[12] = ack;
    head_tag[13] = check_sum(head_tag, 13);
    tx_ack(head_tag, 14);
}
#endif

AT(.text.dac_eq_drc.dbg_tx)
static void eq_tx_ack(u8 bank_num, u8 ack, u8 type)
{
    memset(eq_tx_buf, 0, EQ_TX_BUF_LEN);
    if (type) {// 1 drc
        eq_tx_buf[0] = 'D';
    } else {// 0 eq
        eq_tx_buf[0] = 'A';
    }
    eq_tx_buf[1] = bank_num;
    eq_tx_buf[2] = ack;
    eq_tx_buf[3] = check_sum(eq_tx_buf, 3);
    delay_5ms(1);   //延时一段时间再ack
    tx_ack(eq_tx_buf, 4);
}

AT(.text.dac_eq_drc.dbg_tx)
static void eq_tx_version(void)
{
    memset(eq_tx_buf, 0, EQ_TX_BUF_LEN);
    if (eq_dbg_cb.type) {// 1 drc
        memcpy(eq_tx_buf, tbl_drc_version, 10);
    } else {// 0 eq
        memcpy(eq_tx_buf, tbl_eq_version, 10);
    }
    u16 crc = calc_crc(eq_tx_buf, 10, EQ_CRC_SEED);
    eq_tx_buf[10] = crc;
    eq_tx_buf[11] = crc >> 8;
    delay_5ms(1);   //延时一段时间再ack
    tx_ack(eq_tx_buf, 12);
}

AT(.text.dac_eq_drc.dbg_parse_cmd)
void bsp_eq_parse_cmd(void)
{
    if (eq_rx_buf[0] != 'E' || eq_rx_buf[1] != 'Q') {
        return;
    }
    if (eq_rx_buf[2] == '?' && eq_rx_buf[3] == '#') {
        eq_tx_version();
        return;
    }else if (eq_rx_buf[2] == 'S' && (eq_rx_buf[3] == 'P' || eq_rx_buf[3] == 'F')){
        return ;
    }else if (eq_rx_buf[2] == 'S' && (eq_rx_buf[3] == 'M')){
    }


    u8 band_num = eq_rx_buf[6];
    u32 size = little_endian_read_16(eq_rx_buf, 4);
    u32 crc = calc_crc(eq_rx_buf, size+4, EQ_CRC_SEED);
    if (crc == little_endian_read_16(eq_rx_buf, 4+size)) {
        eq_tx_ack(band_num, 0, eq_dbg_cb.type);
    } else {
        eq_tx_ack(band_num, 1, eq_dbg_cb.type);
        return;
    }


    if (crc == eq_dbg_cb.pre_crc) {
        return;
    }

    eq_dbg_cb.pre_crc = crc;

    if (eq_dbg_cb.type == 0) {//EQ
        u8 band_cnt = eq_rx_buf[7];

        mic_eq_set_by_param(band_cnt, (u32 *)&eq_rx_buf[14]);

#if (UART0_PRINTF_SEL != PRINTF_NONE)
        printf("1234%08x\n", little_endian_read_32(eq_rx_buf, 14));
        u8 k;
        u32 *ptr = (u32 *)&eq_rx_buf[18];
        for (k = 0; k < band_cnt*5; k++) {
            printf("%08x", *ptr++);
            if (k % 5 == 4) {
                printf("\n");
            } else {
                printf(" ");
            }
        }
#endif
    }else if (eq_dbg_cb.type == 1) {//DRC
#if (UART0_PRINTF_SEL != PRINTF_NONE)
        u32 *ptr = (u32 *)&eq_rx_buf[10];
        for (int i = 0; i < 10; i++) {
            printf("%08x\n", ptr[i]);
        }
        printf("\n");
#endif
    }
    memset(eq_rx_buf, 0, EQ_BUFFER_LEN);
}

AT(.com_huart.dac_eq_drc.dbg_rx)
u8 bsp_eq_rx_done(u8* rx_buf)
{
    if((rx_buf[0]=='E')&&(rx_buf[1]=='Q')){
        msg_enqueue(EVT_ONLINE_SET_EQ);
        eq_dbg_cb.rx_type = 1;
    }else if ( ((rx_buf[0]=='L')&&(rx_buf[1]=='E')&&(rx_buf[2]=='M')&&(rx_buf[3]=='I')&&(rx_buf[4]=='C') )) {
        msg_enqueue(EVT_ONLINE_SET_EFFECT);
        eq_dbg_cb.rx_type = 1;
    }
    return eq_dbg_cb.rx_type;
}

AT(.text.dac_eq_drc.dbg_init)
void eq_dbg_init(void)
{
    memset(&eq_dbg_cb, 0, sizeof(eq_dbg_cb_t));

}
#endif
